#include "../test_helpers.h"
#include "MessageProducer.h"
#include <spdlog/spdlog.h>
#include "MessageConsumer.h"
#include <gtest/gtest.h>
#include <chrono>
#include <atomic>

using namespace MessageFramework;
using namespace MessageFramework::Test;

class ThroughputTest : public RedisTestBase {
protected:
    void SetUp() override {
        RedisTestBase::SetUp();
        spdlog::set_level(spdlog::level::warn);  // 减少日志输出
    }
};

TEST_F(ThroughputTest, ProducerThroughput) {
    MessageProducer::Config config;
    config.publisherId = "ThroughputTest";
    config.secretKey = "test-secret-key-32bytes-long!!!";
    config.autoSign = true;

    MessageProducer producer(redisPool, config);

    std::string streamName = testStreamPrefix + "_throughput";
    const int messageCount = 10000;
    std::string body = TestHelpers::createTestBody();

    auto start = std::chrono::high_resolution_clock::now();

    // 发送消息
    for (int i = 0; i < messageCount; ++i) {
        producer.send(streamName, "TestMessage", body);
    }

    auto end = std::chrono::high_resolution_clock::now();
    auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();

    double throughput = (messageCount * 1000.0) / duration;

    std::cout << "\n性能测试结果：" << std::endl;
    std::cout << "  消息数量: " << messageCount << std::endl;
    std::cout << "  总耗时: " << duration << " ms" << std::endl;
    std::cout << "  吞吐量: " << std::fixed << std::setprecision(2) << throughput << " msg/s" << std::endl;
    std::cout << "  平均延迟: " << (duration * 1.0 / messageCount) << " ms" << std::endl;

    // 验证吞吐量至少达到1000 msg/s
    EXPECT_GT(throughput, 1000.0);
}

TEST_F(ThroughputTest, ConsumerThroughput) {
    std::string streamName = testStreamPrefix + "_consumer_throughput";
    const int messageCount = 5000;

    // 预先发送消息
    MessageProducer::Config producerConfig;
    producerConfig.publisherId = "ThroughputTest";
    producerConfig.secretKey = "test-secret-key-32bytes-long!!!";

    MessageProducer producer(redisPool, producerConfig);

    for (int i = 0; i < messageCount; ++i) {
        producer.send(streamName, "TestMessage", TestHelpers::createTestBody());
    }

    // 创建消费者
    MessageConsumer::Config consumerConfig;
    consumerConfig.streamName = streamName;
    consumerConfig.consumerGroup = testStreamPrefix + "_group";
    consumerConfig.consumerId = "ThroughputConsumer";
    consumerConfig.batchSize = 100;  // 批量读取
    consumerConfig.autoCreateGroup = true;

    auto dispatcher = std::make_shared<MessageDispatcher>();
    MessageDispatcher::Config dispatcherConfig;
    dispatcherConfig.secretKey = "test-secret-key-32bytes-long!!!";
    dispatcher->setConfig(dispatcherConfig);

    std::atomic<int> processedCount(0);
    auto handler = std::make_shared<MockHandler>("TestMessage");
    dispatcher->registerHandler("TestMessage", handler);

    MessageConsumer consumer(redisPool, consumerConfig, dispatcher);

    auto start = std::chrono::high_resolution_clock::now();
    consumer.start();

    // 等待所有消息被消费
    bool finished = TestHelpers::waitFor([&]() {
        return handler->getCallCount() >= messageCount;
    }, 30000);  // 30秒超时

    auto end = std::chrono::high_resolution_clock::now();
    consumer.stop();

    EXPECT_TRUE(finished);

    auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
    double throughput = (messageCount * 1000.0) / duration;

    std::cout << "\n消费者性能测试结果：" << std::endl;
    std::cout << "  消息数量: " << messageCount << std::endl;
    std::cout << "  总耗时: " << duration << " ms" << std::endl;
    std::cout << "  吞吐量: " << std::fixed << std::setprecision(2) << throughput << " msg/s" << std::endl;
    std::cout << "  处理速率: " << handler->getCallCount() << " / " << messageCount << std::endl;
}

int main(int argc, char** argv) {
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}
